<?php
	class Acompra extends CI_Controller
	{
		function index()
		{
			session_start();
			if(isset($_SESSION["host"]))
			{
				$this->load->view("agregar/acompra");
			}
			else
			{
				redirect("/");
			}
		}
		function agrcmp()
		{
			session_start();
			if(isset($_SESSION["host"]))
			{
				$socket=fsockopen($_SESSION["host"],(int)$_SESSION["puerto"]);
				if($socket)
				{
					fputs($socket,$_SESSION["id"]);
					time_nanosleep(0,500000000);
					fputs($socket,"addcmp".str_pad($this->input->post("iddisco"),4,'0',STR_PAD_LEFT).str_pad($this->input->post("cantidad"),4,' ',STR_PAD_LEFT));
					$msj=fread($socket,1024);
					if($msj=="Error1")
					{
						echo "ID de disco no existe";
					}
					else if($msj=="Error2")
					{
						echo "Stock insuficiente";
					}
					else
					{
						echo "Compra Exitosa";
					}
				}
				else
				{
					print_r("Error");
				}
			}
			else
			{
				redirect("/principal/login");
			}
		}

	}
?>